11.1 Getting Started with Variable
Here we have three programs that all look similar. The only difference is how many beats the duration of the note.

Try it using Scratch and listen to how different they sound. The second one should sound faster than the first one and the last one faster than the rest. What do you think is the role of beats that we are using? The rate of beat in a note is the tempo of that note. In the Sounds tab, we can see something called Tempo. Find the tempo block and click the check box next to it. This lets you see the value of the variable "tempo" on the stage.

When you click on the box Tempo, it will show you the variable tempo on the top left corner on the output screen.

Right now, it is set at 60 which means that it is 60 beats per minute or BPM.

Listen to the script block above, where it plays each note for one beat. It takes about a second because right now our tempo is 60 beats per minute, as we have 60 seconds in a minute. We can do two things to the tempo: we can set the tempo, and we can change the tempo.

Setting the tempo means, to give an initial value to tempo. Changing tempo means adjusting it to a new value by adding or subtracting a number from the previous value. Look what happens when you double click on the block .
Can you see that this changed to 80 on the top left corner of the stage? If you click on it again, it goes to 100. If you click on it again, it goes to 120. This is an increment in the value. Similarly, we can decrease by changing the value with a negative number, which is a decrement. Note that the set block behaves differently.
If you click on
, the value of tempo jumps right back down to 60. No matter what it was before, the Set block changes it to the number given in the Set block. You can use these to set the value of tempo. For example, set the tempo to 120. Now the note will sound faster.
Remember this important difference between Set and Change. Set ignores what the value of tempo was before, and sets it to the given value, while Change increments or decrements it based on the given value. Try changing the tempo to something higher than the original tempo. What does it sound like? Attempt the example below in Scratch.

If you drag this block to the script side in scratch and double-click on it, it's going to ignore whatever value the tempo is at and will just set it to 100.

The first block in the above script sets tempo to “60” and the next one will changes it by 20, i.e., 60 plus 20. Try to assemble the following blocks and then double-click.

Let us try something a bit different.

The last block, ignores the code before and resets the value of tempo back to 60. Try it out in Scratch. Tempo is a variable in Scratch. The name “tempo” is used a placeholder for the value we want to store or use. Programs use variables to store the result of calculations or user input.
Excercise
- What is the value of tempo after we run this script?
- What is the value of tempo after we run this script?
- What is the value of tempo after we run this script?
- What is the value of tempo after we run this script?
11.2 Variables with Repeat
We are going to start by looking at changing tempo within a repeat block. First, let us look at it without repeat, using the code block below.

If we run the block, the value of tempo starts from 60, goes up by ten 4 times and we end up with 100. The same result is obtained by changing tempo within in a repeat block.

Let us look at some examples. Can you tell what the value of tempo will be after the following script is run?

Let us try to think about how the computer would process this. We know that Set block ignores all the previous values of the tempo and changes it to the one mentioned in the Set block. So in the first block tempo will be set to ‘60’. Next, the repeat block having the change of 5 in it, will change the value of tempo. This will happen 10 times, adding 5 each time. The final value of tempo after this block of code will be “110”. Try to work out the value after the following script runs.

When programming, we should keep track of exactly which instruction is executed and in what order. Let us look at another script with tempo in Scratch.

The first block sets the tempo to 60. The next repeat block with value 4, will change the tempo by 10. This means by the end of the first repeat block, value of tempo will be 60 plus 40. The second repeat block with value 5, changes the tempo by negative three. Can you work out what will be the value of tempo at the end of this block? The last repeat block with value 10, changes the tempo by two. It is a helpful strategy to try different parts of the script bit by bit to see if it is actually doing the required task. Now consider joining these blocks and calculate what will be the value of the complete code. Try running it on Scratch and see if the value of tempo matches what you have already calculated.
Two important things to remember are: one, it's really important to know what line of code is being executed, and two it will be really helpful for debugging a script if you run different parts of it separately. If you have a more complicated script, it can be a good idea to duplicate your script, so you do not lose any of your work. Keep practicing to get better at programming! Try the following script for yourself.

Excercise
- Consider the three scripts to below.
Script 1 Script 2 Script 3 - What is the value of tempo after each script is executed?
Script 1 ____________ Script 2 ____________ Script 3 ____________
- Do any of these scripts have identical results? If so, which ones do the same thing?
- No
- Yes, 1 and 2
- yes, 1 and 3
- yes, 2 and 3
- yes, 1,2 and 3
- What is the value of tempo after each script is executed?
- Consider the scripts given below:
- The tempo after we run Script A is ____________
- The tempo after we run Script B is ____________
11.3 Creating New Variables in Scratch
We are going to learn how to play a sequence of notes.

Make this program in Scratch and watch how the tempo of the drum changes. Every time we play the drum, we play it for one beat, but the tempo changes the time duration between the beats. So it sounds faster and faster. We are going to play a sequence of notes and to do that we are going to create own variable. To make a variable in Scratch, click on the Data tab. There you can see a button, “Make a Variable”. You can give any name to your variable, for example, here we will name it “note”.


Just like tempo, this variable can be set and changed to the value of our choice with the blocks that appear as soon as we name the variable. The value of the variable is visible at the top left corner of the stage. We have to keep in mind that variable holds a value that can be set and changed along the flow of the program. It is also usable in different blocks throughout the script. Let us consider the example below.

In the first code block on the left in Figure 103, we play every note from 60 to 69 one by one, using a separate code block for each note. This requires that we combine ten different code blocks. There must be a better way of doing this. The obvious idea that comes to mind is to use repeat blocks, but it not clear how, since we play a different note each time. This is exactly the kind of situation where variables can help. In the middle example of Figure 103, we use our newly created variable “note”. We use a change note block to increase the variable value and then use the play note block. The note value played is selected by using the block for our variable “note”, which we drag and drop in the place of the note value. At first glance, this may seem counterproductive. The number of code blocks we use has now gone up to 21. The advantage is that now exactly the same two code blocks repeat themselves 10 times. We play a note given by the value of the variable “note” and then change the value of the variable note by one. This is exactly the kind of code that can benefit by using a repeat block. We take the two blocks and put them inside a repeat block that is set to ten.
11.4 Spot the Bug
The first two scripts in Figure 103 play the notes 60, 61, 62, 63, 64, 65, 66, 67, 68 and 69. What notes does the script using repeat play? This error is called the “off by one error”. Can you figure out how to fix the bug? Figure 104 shown the solution.


We can perform math operations on variables directly as well. This allows for more a powerful and flexible way to control the value of variables. In the operators tab, the first four green blocks are for addition, subtraction, multiplication and division. You can directly provide numbers as input or use these blocks with variables. Try them out!
Let us see how we can use these blocks with variables. Consider the variable ‘my number’ in the script below. The green block simply adds 3 to the value stored in the variable “my number”.
What do you think equals to after we run the script below?

Excercise
- Suppose X is a variable created in Scratch. What is the value of X after this script executes?
- What is the value of X after the following script executes?
- What does X equal after we double click the script below?
- What does X equal after we double click the script below?
11.5 Why Order Matters in Repeat
Errors like the “off by one” error we saw in the last section are common when programming. Consider the two similar looking scripts in Figure 106. The only difference between the two examples is the order of the change and play note blocks inside repeat. This small difference is important because it results in a different output. The first example plays the note first and then changes the value of the variable by two, while the second example first change the value of the variable “note” by two and then plays the note. Due to this change in order, the final output sequence played by the two scripts is different.

Excercise
- We have seen an example of a script that plays every note between 60 and 70. Now, write a script that plays every other note from 40 to 90. So, it should play the notes 40, 42, 44,…, 88, 90.
- Make a script using repeat and variables, that plays each note from 60 to 70 and then backwards from 70 down to 60 again.
- Write a script that make the sprite cat count down from ten to one.
Use a variable named
. The expected output when the cat say 10 looks as follows.
Hints
- You need to use the repeat block.
- You need to use
, in the “Looks” tab.
- Replace “Hello!” with your variable
.
- There are three different ways
can be decreased by 1: .
11.6 Tracing Repeats
A common technique used to check whether a given code is working correctly is to “trace” through the different values all variables take when the code is executed. This way we can keep track of exactly when a variable changes value. This is a good way to find errors similar to “off by one”. Consider the following code.

How does “tracing” work? For the above script, we can make two columns, as shown in the Figure. One keeps track on the value in the variable “my number”, while the other actually tracks what the cat says based on the current value of the variable. We now work through the code line by line and write down the values for both columns. Note that we do this manually without actually executing the code in Scratch. The result of the trace for the above script is shown next to it.
Now, trace through the following code and write out the number of the notes that play when the script runs.

Write out the number of notes that will play when the following script runs. Note that now there are multiple change blocks within the repeat block.

A slightly trickier example using nested repeat blocks is given below. First trace through the code yourself to figure out the values “my number” takes and the numbers the cat says out. Check your list against the solution.

Excercise
- Suppose we create a new variable “points”. What values does the variable "points" take on when the following script is run? Make a list of all the values, in order, separated by spaces.
- Consider the scripts below:
- What is the first number that Script A says when we run it?
- What is the first number that Script B says when we run it?
- What is the last number that Script A says when we run it?
- What is the last number that Script B says when we run it?
- List the numbers the sprite says when we run this script below. Separate the numbers with spaces.
- List the numbers the sprite says when we run this script below. Separate the numbers with spaces.
- List the numbers the sprite says when we run this script below. Separate the numbers with spaces.
- List all the numbers the sprite says when we run the script below.